package slidingWindow;

//https://leetcode.cn/problems/max-consecutive-ones-iii/
public class l_1004 {
    public int longestOnes(int[] nums, int k) {
        int n = nums.length, count = 0, ans = 0;
        //维护滑动窗口内0的个数, 根据是否大于k来选择左右指针的移动
        for(int left = 0, right = 0; right < n; ++right) {
            if(nums[right] == 0) ++count;
            while(count > k) {
                if(nums[left++] == 0) --count;
            }
            ans = Math.max(ans, right - left + 1);
        }
        return ans;
    }
}
